/**
 * @component base.js
 * @description 基础方法库
 * @time 2019/3/13
 * @author JUSTIN
 */
export function delay(time = 300) {
  return new Promise((resolve) => { return setTimeout(() => { return resolve(time); }, time); });
}

/**
 * 对象转换成 key value数组
 * @param {*} obj
 */
export const mapToArray = (obj) => { return Object.keys(obj).map((key) => { return obj[key]; }); };


/**
 * 序列化模式
 * @param {Number} commMode
 * @return {*}
 */
export function formatCommMode(commMode) {
  commMode = Number(commMode);
  if (commMode === 1) {
    return {
      name: '现金网',
      style: { backgroundColor: 'rgba(255, 204, 153, 1)' },
    };
  }
  if (commMode === 2) {
    return {
      name: '信用网',
      style: { backgroundColor: 'rgba(153, 204, 255, 1)' },
    };
  }
  return {
    name: '',
    style: { backgroundColor: 'rgba(228, 228, 228, 1)' },
  };
}

/**
 * 递归查询tree path
 * @param {Array} list
 * @param {String} value
 * @param {String} equalKey {name/uid}
 * @param {String} returnKey {name/uid}
 * @return [*] 从顶层到当前层级
 */
export function getTreePathList(list, value, equalKey = 'name', returnKey = 'uid') {
  for (let i = 0; i < list.length; i ++) {
    const { children = [], [equalKey]: name, [returnKey]: uid } = list[i];
    if (name === value) {
      return [uid];
    }
    if (children && children.length) {
      const res = getTreePathList(children, value, equalKey, returnKey);
      if (res) {
        return [uid, res].flat();
      }
    }
  }
}
